package cc.linker.steplink.crm.merchant.controller;

import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.NewMallService;
import cc.linker.steplink.crm.response.GlobalException;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 小B商品
 */
@Slf4j
@RestController
@RequestMapping("/xb/shop/goods")
public class XbShopGoodsController {

    @Autowired
    private NewMallService newMallService;
    @Autowired
    private ShopGoodsController shopGoodsController;
    @Autowired
    private UserInfoUtil userInfoUtil;

    /**
     * 商品列表
     *
     * @param pageIndex 页码
     * @param pageSize  页面大小
     * @param status    状态
     * @param key       关键字
     */
    @PostMapping("/list")
    public Object getGoodsList(@RequestBody Map<String, Object> params, HttpServletRequest request) {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String adminId = userinfo.getAdminId();
        String orgId = userinfo.getOrgId();
        params.put("adminId", adminId);
        params.put("orgId", orgId);
        return newMallService.getGoodsListByUserId(params);
    }

    /**
     * 商品新增编辑
     *
     * @param goodsBanner  商品图片(数组)
     * @param goodsContent 商品简介
     * @param goodsCount   商品数量
     * @param goodsCover   商品封面
     * @param goodsFreight 运费
     * @param goodsGroup   商品分组
     * @param goodsName    商品名
     * @param goodsSpec    规格
     * @param goodsStatus  商品状态
     * @param isFreight    是否包邮
     * @param moneyAmount  价格
     * @param originalCost 原价
     * @param sku          规格值
     * @param rebate       返利
     * @param rebateRf     推荐返利(Recommendation fee)
     * @param videoContent 视频内容
     * @param videoCover   视频封面
     * @param videoResId   视频资源id
     * @param shareTitle   分享标题
     * @param shareInfo    分享简介
     * @param quota        是否限购0-不限 1-限制
     * @param quotaCycle   限购周期0-终身，1-日，2-周，3-月，4-近x天
     * @param quotaNum     限购件数
     * @param quotaDay     限购天数
     */
    @PostMapping("/save")
    public void save(@RequestBody Map<String, Object> params, HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = userinfo.getOrgId();
        String adminId = userinfo.getAdminId();
        params.put("orgId", orgId);
        params.put("adminId", adminId);
        shopGoodsController.checkGoodsRewetting(params,request);
        if(null!=params.get("goodsRemark")){
            params.put("goodsRemark", JSON.toJSONString(params.get("goodsRemark")));
        }
        newMallService.savexb(params);
    }

    /**
     * @param goodsId 商品id
     * @param mine    商品类型0-自有1-联盟2-共享
     */
    @PostMapping("/updateStatus")
    public void updateStatus(@RequestBody Map<String, Object> params, HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String adminId = userinfo.getAdminId();
        if(null == params.get("param")){
            throw GlobalException.build("至少选择一个商品");
        }
        List<Map<String, Object>> param = (List<Map<String, Object>>)params.get("param");
        for(Map<String, Object> pa:param){
            pa.put("adminId", adminId);
            newMallService.updateStatus(pa);
        }
    }

    /**
     * 判断用户是否可以新增、上架商品
     */
    @GetMapping("/judge")
    public void judge(HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        Map<String, Object> params = new HashMap<>();
        String adminId = userinfo.getAdminId();
        params.put("adminId", adminId);
        String orgId = userinfo.getOrgId();
        params.put("orgId", orgId);
        Boolean ifSales = userinfo.getSaler();
        if (ifSales) {
            // 小b
            newMallService.judgeStateOfAdmin(params);
        } else {
            // 大b
            // 发布不做判断
            //newMallService.judgeStateOfAdminOrg(params);
        }
        // 判断商品创建数量是否到达上限
        newMallService.judgeGoodsLimitByOrgId(params);
    }

}
